//
//  ZHFPhotoViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFPhotoViewController.h"

#import "Resources.h"
#import "ZHFPhoto.h"
#import "ZHFPhotoCell.h"
#import "ZHFPhotoDetailViewController.h"
#import "MBProgressHUD.h"

@interface ZHFPhotoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,WSPhotoDetailViewControllerDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, retain) ZHFPhotoDetailViewController *photoVC;

@property (nonatomic, retain) MBProgressHUD *mbBarPrpgressV;


@end

@implementation ZHFPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.array = [NSMutableArray array];
    self.page = 1;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / 3, (SCREEN_WIDTH - 20) / 3);
    flowlayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowlayout.minimumLineSpacing = 5;
    flowlayout.minimumInteritemSpacing = 5;
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:flowlayout];
    self.collectionView.backgroundColor = self.view.backgroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ZHFPhotoCell class] forCellWithReuseIdentifier:@"photoCell"];
    [self.view addSubview:self.collectionView];
    
    self.photoVC = [[ZHFPhotoDetailViewController alloc] init];
    
    [self.view bringSubviewToFront:self.getDataBtn];
    self.mbBarPrpgressV = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self AFNWorking];
}

#pragma mark - collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    cell.photo = [self.array objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //指定代理人
    self.photoVC.delegate = self;
    self.photoVC.row = indexPath.row;
    
    self.photoVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:self.photoVC animated:YES completion:nil];
}

#pragma mark 模态返回代理方法
//6.实现协议方法
- (void)scrollCellWithIndex:(NSInteger)index {
    
    self.collectionView.contentOffset = CGPointMake(0, index / 3 * (SCREEN_WIDTH / 3));
}

- (void)netwotking {
    
    self.page++;
    [self AFNWorking];
}

#pragma mark - AFN网络请求
- (void)AFNWorking {
    
    self.getDataBtn.hidden = YES;
    [self.mbBarPrpgressV show:YES];
    NSString *strUrl = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@/photos.json?per_page=20&page=%ld", self.Id, self.page];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSArray *bigArray = result;
        for (NSDictionary *dic in bigArray) {
            ZHFPhoto *photo = [ZHFPhoto modelWithDictionary:dic];
            [self.array addObject:photo];
        }
        
        [self.mbBarPrpgressV hide:YES];
        self.photoVC.array = self.array;
        [self.collectionView reloadData];
        
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (self.array.count == 0) {
                self.getDataBtn.hidden = NO;
            }
            [self.mbBarPrpgressV hide:YES];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

#pragma mark 点击刷新
- (void)tapAction:(UITapGestureRecognizer *)button {
    
    [self AFNWorking];
}

#pragma mark 滑动刷新
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //当scrollView偏移量达到末尾第二或第一行时刷新
    if (scrollView.contentOffset.y > (self.page * 20 / 3 - 1) * (SCREEN_WIDTH / 3) - (SCREEN_HEIGHT - 64)) {
        self.page++;
        [self AFNWorking];
    }
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
